package cn.iocoder.yudao.module.handguard.controller.app.userbasetable16;

import cn.iocoder.yudao.framework.common.util.http.HttpUtils;
import cn.iocoder.yudao.module.handguard.service.zoneprice.ZonePriceService;
import cn.iocoder.yudao.module.handguard.tool.http.VCodeHttpUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.math.BigDecimal;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUser;

import cn.iocoder.yudao.module.handguard.controller.app.userbasetable16.vo.*;
import cn.iocoder.yudao.module.handguard.dal.dataobject.userbasetable16.UserBaseTable16DO;
import cn.iocoder.yudao.module.handguard.convert.userbasetable16.UserBaseTable16Convert;
import cn.iocoder.yudao.module.handguard.service.userbasetable16.UserBaseTable16Service;

@Tag(name = "小程序 - 完成住宅二次抵押")
@RestController
@RequestMapping("/hutai/user-base-table16")
@Validated
public class UserBaseTable16Controller {

    @Resource
    private UserBaseTable16Service userBaseTable16Service;

    @Resource
    private ZonePriceService zonePriceService;

    @PostMapping("/create")
    @Operation(summary = "创建完成住宅二次抵押")
    public CommonResult<Long> createUserBaseTable16(@Valid @RequestBody UserBaseTable16CreateReqVO createReqVO) {
        if (StringUtils.isEmpty(createReqVO.getCellName())){
            return  error(500,"小区名称不能为空");
        }
        //根据小区名字获取评估价
        //String valuationPrice = VCodeHttpUtils.getBkeHousingResForSearch(createReqVO.getCellName());
        String valuationPrice = zonePriceService.getZonePriceValuation(createReqVO.getArea(),createReqVO.getCellName());
        if (StringUtils.isNotEmpty(valuationPrice)) {
            createReqVO.setValuationPrice(new BigDecimal(valuationPrice));
            return success(userBaseTable16Service.createUserBaseTable16(createReqVO));
        }else {
            return error(500,"该小区暂无评估数据\n " +
                    "我们将继续努力");
        }
    }

    @PutMapping("/update")
    @Operation(summary = "更新完成住宅二次抵押")
    public CommonResult<Boolean> updateUserBaseTable16(@Valid @RequestBody UserBaseTable16UpdateReqVO updateReqVO) {
        if (StringUtils.isEmpty(updateReqVO.getCellName())){
            return  error(500,"小区名称不能为空");
        }
        //String valuationPrice = VCodeHttpUtils.getBkeHousingResForSearch(updateReqVO.getCellName());
        String valuationPrice = zonePriceService.getZonePriceValuation(updateReqVO.getArea(),updateReqVO.getCellName());
        if (StringUtils.isNotEmpty(valuationPrice)) {
            updateReqVO.setValuationPrice(new BigDecimal(valuationPrice));
            userBaseTable16Service.updateUserBaseTable16(updateReqVO);
            return success(true);
        }else {
            return error(500,"该小区暂无评估数据\n " +
                    "我们将继续努力");
        }
    }

//    @DeleteMapping("/delete")
//    @Operation(summary = "删除完成住宅二次抵押")
//    @Parameter(name = "id", description = "编号", required = true)
//    public CommonResult<Boolean> deleteUserBaseTable16(@RequestParam("id") Long id) {
//        userBaseTable16Service.deleteUserBaseTable16(id);
//        return success(true);
//    }

    @GetMapping("/get")
    @Operation(summary = "获得完成住宅二次抵押")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<UserBaseTable16RespVO> getUserBaseTable16(@RequestParam("id") Long id) {
        UserBaseTable16DO userBaseTable16 = userBaseTable16Service.getUserBaseTable16(id);
        return success(UserBaseTable16Convert.INSTANCE.convert(userBaseTable16));
    }

    @GetMapping("/list")
    @Operation(summary = "获得完成住宅二次抵押列表")
    public CommonResult<List<UserBaseTable16RespVO>> getUserBaseTable16List(Long ui) {
        Long userId = ui;
        //当前用户id
        if (userId <= 0l) {
            userId = getLoginUser().getId();
        }
        List<UserBaseTable16DO> list = userBaseTable16Service.getUserBaseTable16List(userId);
        return success(UserBaseTable16Convert.INSTANCE.convertList(list));
    }

//    @GetMapping("/page")
//    @Operation(summary = "获得完成住宅二次抵押分页")
//    @PreAuthorize("@ss.hasPermission('hutai:user-base-table16:query')")
//    public CommonResult<PageResult<UserBaseTable16RespVO>> getUserBaseTable16Page(@Valid UserBaseTable16PageReqVO pageVO) {
//        PageResult<UserBaseTable16DO> pageResult = userBaseTable16Service.getUserBaseTable16Page(pageVO);
//        return success(UserBaseTable16Convert.INSTANCE.convertPage(pageResult));
//    }
//
//    @GetMapping("/export-excel")
//    @Operation(summary = "导出完成住宅二次抵押 Excel")
//    @PreAuthorize("@ss.hasPermission('hutai:user-base-table16:export')")
//    @OperateLog(type = EXPORT)
//    public void exportUserBaseTable16Excel(@Valid UserBaseTable16ExportReqVO exportReqVO,
//              HttpServletResponse response) throws IOException {
//        List<UserBaseTable16DO> list = userBaseTable16Service.getUserBaseTable16List(exportReqVO);
//        // 导出 Excel
//        List<UserBaseTable16ExcelVO> datas = UserBaseTable16Convert.INSTANCE.convertList02(list);
//        ExcelUtils.write(response, "完成住宅二次抵押.xls", "数据", UserBaseTable16ExcelVO.class, datas);
//    }

}
